

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.json.*;



public class XMLParser {
	public final static int LOGIN = 1;
	public final static int BUY = 2;
	private LotteryOrder lo = null;
	
	private int count = 0;
	private int pagecount = 0;
	private int totalmoney = 0;
	private int permoney = 0;
	private ArrayList<LotteryRecord> recordlist = null;
	private ArrayList<AccountRecord> accountlist = null;
	
	public XMLParser(){
		recordlist = new ArrayList<LotteryRecord>();
		accountlist = new ArrayList<AccountRecord>();
	}
	
	public XMLParser(LotteryOrder lo){
		this.lo = lo;
	}
	/**
	 * 
	 * @param act 行為。1 login, 2 上傳資料, 3 取得記錄
	 * @param xml
	 * @return
	 */
	public boolean parse(int act, String xml) {
		
		System.out.println("XMLParser"+xml);
		InputStream in;
		
		
		try {
			in = new ByteArrayInputStream(xml.getBytes("ISO-8859-1"));
			DocumentBuilder builder = DocumentBuilderFactory.newInstance()
					.newDocumentBuilder();
			Document doc = builder.parse(in);
			
			switch(act){			
				
				case 2://post data
					in = new ByteArrayInputStream(xml.getBytes("UTF-8"));
					doc = builder.parse(in);
					boolean postok = false;
					if (doc != null) {
						NodeList nl = doc.getElementsByTagName("status");
						if (nl.getLength() > 0) {
							Node node = nl.item(0);
							
							if(node.getTextContent().equals("ok")){
								postok = true;
							}else if(node.getTextContent().equals("fail")){
								postok = false;
							}							
						}
						nl = doc.getElementsByTagName("message");
						if (nl.getLength() > 0) {
							Node node = nl.item(0);
							lo.setImageURL(node.getTextContent());
						}	
						nl = doc.getElementsByTagName("url");
						if (nl.getLength() > 0) {
							Node node = nl.item(0);
							lo.setImgOrderURL(node.getTextContent());
						}
					}					
					return postok;
				case 3://get lottery money
					boolean getOK = false;
					if (doc != null) {
						NodeList nl = doc.getElementsByTagName("status");
						if (nl.getLength() > 0) {
							Node node = nl.item(0);
							
							if(node.getTextContent().equals("ok")){
								getOK = true;
							}else if(node.getTextContent().equals("fail")){
								getOK = false;
							}							
						}
						nl = doc.getElementsByTagName("message");
						if (nl.getLength() > 0) {
							Node node = nl.item(0);
							permoney = Integer.parseInt(node.getTextContent());
						}	
						
					}
					return getOK;
					
				case 4: // day record
					in = new ByteArrayInputStream(xml.getBytes("UTF-8"));
					doc = builder.parse(in);
					boolean getok = false;
					if (doc != null) {
						NodeList nl = doc.getElementsByTagName("status");
						if (nl.getLength() > 0) {
							Node node = nl.item(0);
							
							if(node.getTextContent().equals("ok")){
								getok = true;
							}else if(node.getTextContent().equals("fail")){
								getok = false;
							}							
						}
						nl = doc.getElementsByTagName("message");
						if (nl.getLength() > 0) {
							Node node = nl.item(0);
						}	
						nl = doc.getElementsByTagName("count");
						if (nl.getLength() > 0) {
							Node node = nl.item(0);
							count = Integer.parseInt(node.getTextContent());
						}
						
						nl = doc.getElementsByTagName("pagecount");
						if (nl.getLength() > 0) {
							Node node = nl.item(0);
							pagecount = Integer.parseInt(node.getTextContent());
							
						}
						
						nl = doc.getElementsByTagName("paymoney");
						if (nl.getLength() > 0) {
							Node node = nl.item(0);
							totalmoney = Integer.parseInt(node.getTextContent());
							
						}
						
						nl = doc.getElementsByTagName("listdata");
						if (nl.getLength() > 0) {		
							for(int i = 0; i < nl.getLength(); i++){
								LotteryRecord lr = new LotteryRecord();
								Node node = nl.item(i);
								Element element = (Element) node;
								
								try{
									NodeList nl2 = element.getElementsByTagName("id");
									if(nl2.getLength() > 0)lr.setOrder_id(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("status");
									if(nl2.getLength() > 0)lr.setStatus(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("createdate");
									if(nl2.getLength() > 0)lr.setCreateDate(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("paydate");
									if(nl2.getLength() > 0)lr.setPayDate(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("gamekind");
									if(nl2.getLength() > 0)lr.setGameKind(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("iskuai");
									if(nl2.getLength() > 0)lr.setIsKUAI(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("playkind1");
									if(nl2.getLength() > 0)lr.setPlayKind1(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("code1");
									if(nl2.getLength() > 0)lr.setCode1(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("playkind2");
									if(nl2.getLength() > 0)lr.setPlaykind2(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("code2");
									if(nl2.getLength() > 0)lr.setCode2(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("num");
									if(nl2.getLength() > 0)lr.setNum(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("paymoney");
									if(nl2.getLength() > 0)lr.setPayMoney(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("pic");
									if(nl2.getLength() > 0)lr.setURL(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("pic1");
									if(nl2.getLength() > 0)lr.setURL1(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("pic2");
									if(nl2.getLength() > 0)lr.setURL2(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("pic3");
									if(nl2.getLength() > 0)lr.setURL3(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("getstatus");
									if(nl2.getLength() > 0)lr.setGetStatus(nl2.item(0).getTextContent());
																		
									recordlist.add(lr);
								}catch(ArrayIndexOutOfBoundsException e){
									System.err.println("XMLParser:" + e.toString());
									continue;
								}
							}							
						}
					}					
					return getok;
				case 5:
					in = new ByteArrayInputStream(xml.getBytes("ISO-8859-1"));
					doc = builder.parse(in);
					boolean getrecordok = false;
					if (doc != null) {
						
						NodeList nl = doc.getElementsByTagName("status");
						if (nl.getLength() > 0) {
							Node node = nl.item(0);
							
							if(node.getTextContent().equals("ok")){
								getrecordok = true;
							}else if(node.getTextContent().equals("fail")){
								getrecordok = false;
							}							
						}
						nl = doc.getElementsByTagName("message");
						if (nl.getLength() > 0) {
							Node node = nl.item(0);							
						}	
						
						nl = doc.getElementsByTagName("count");
						if (nl.getLength() > 0) {
							for(int i = 0; i < nl.getLength(); i++){
								AccountRecord ar = new AccountRecord();
								Node node = nl.item(i);
								Element element = (Element) node;
								
								try{
									NodeList nl2 = element.getElementsByTagName("date");
									if(nl2.getLength() > 0)ar.setDate(nl2.item(0).getTextContent());
									nl2 = element.getElementsByTagName("money");
									if(nl2.getLength() > 0)ar.setMoney(nl2.item(0).getTextContent());
									accountlist.add(ar);
								}catch(ArrayIndexOutOfBoundsException e){
									System.err.println("XMLParser:"+ e.toString());
									continue;
								}
							}
						}
						
						return getrecordok;
					}
				default:
					return false;
			}
			
		} catch (IllegalStateException e) {
			System.err.println("XMLParser1:"+e.toString());
			return false;
		} catch (IOException e) {
			System.err.println("XMLParser2:"+e.toString());
			return false;
		} catch (ParserConfigurationException e) {
			System.err.println("XMLParser3:"+ e.toString());
			return false;
		} catch (SAXException e) {
			System.err.println("XMLParser4:"+ e.toString());
			return false;
		}
		 
	}
	
	public ArrayList<LotteryRecord> getRecordList(){
		return recordlist;
	}
	
	public int getLotteryMoney(){
		return permoney;
	}
	
	public int getPageSum(){
		return pagecount;
	}
	
	public int getTotalMoney(){
		return totalmoney;
	}
	
	public ArrayList<AccountRecord> getAccountList(){
		return accountlist;
	}
}
